﻿namespace BEGDC.Utility.Extensions
{
    using System;
    using System.Linq;
    using System.Text.RegularExpressions;

    public static class StringExtensions
    {
        /// <summary>
        /// Determines whether a string is not null or whitespaces.
        /// </summary>
        /// <param name="input">The input string.</param>
        /// <returns><c>true</c> if not null or whitespace; otherwise, <c>false</c>.</returns>
        public static bool IsNotNullOrWhiteSpace(this string input)
        {
            return input != null && input.Any(c => !char.IsWhiteSpace(c));
        }

        /// <summary>
        /// Checkes whether a string equals another one regardless of their cases.
        /// </summary>
        /// <param name="src">The source string.</param>
        /// <param name="dest">The dest string.</param>
        /// <returns><c>true</c> if equals; otherwise <c>false</c>.</returns>
        public static bool VariantEquals(this string src, string dest)
        {
            return string.Compare(src, dest, StringComparison.InvariantCultureIgnoreCase) == 0;
        }

        /// <summary>
        /// Sanitizes the SQL parameter's VALUE.
        /// Note: ONLY use this method when the dynamic sql string should be used and SqlParamter cannot be a option.
        ///  </summary>
        /// <param name="src">The source string.</param>
        /// <returns>Santized value.</returns>
        public static string SanitizeSqlValue(this string src)
        {
            return src.IsNotNullOrWhiteSpace() ? src.Replace("'", "''") : src;
        }
    }
}